package com.ruibang.glass.etl.handler.sy;


import com.gitee.Jmysy.binlog4j.core.BinlogEvent;
import com.gitee.Jmysy.binlog4j.core.IBinlogEventHandler;
import com.gitee.Jmysy.binlog4j.springboot.starter.annotation.BinlogSubscriber;
import com.ruibang.glass.etl.entity.AlarmHistory;
import com.ruibang.glass.etl.entity.BcStationFxDetail;
import com.ruibang.glass.etl.entity.BcStationXbDetail;
import com.ruibang.glass.etl.mapper.BcStationFxDetailMapper;
import com.ruibang.glass.etl.mapper.GlassAlarmHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;

@BinlogSubscriber(clientName = "sy")
public class SYBCStationFXDetailBinlogEventHandler implements IBinlogEventHandler<BcStationFxDetail> {

    @Autowired
    private BcStationFxDetailMapper bcStationFxDetailMapper;
    @Override
    public void onInsert(BinlogEvent<BcStationFxDetail> event) {
        if(1 > bcStationFxDetailMapper.insert(event.getData())){
            String stringBuilder = event.getDatabase() + "-" +
                    event.getTable() + " insert record fail,data: " + event.getData();
            System.out.println(stringBuilder);
        }else
            System.out.println(event.getDatabase() + "-" +
                    event.getTable() + " 插入数据成功");
    }

    @Override
    public void onUpdate(BinlogEvent<BcStationFxDetail> event)  {
        BcStationFxDetail detail = event.getData();
        if(null == bcStationFxDetailMapper.selectById(detail.getId())){
            bcStationFxDetailMapper.insert(event.getData());
        }else{
            bcStationFxDetailMapper.updateById(detail);
        }
        System.out.println(event.getDatabase() + "-" +
                event.getTable() + " 更新数据成功");
    }

    @Override
    public void onDelete(BinlogEvent<BcStationFxDetail> event) {

        if(1 > bcStationFxDetailMapper.deleteById(event.getData())){
            String stringBuilder = event.getDatabase() + "-" +
                    event.getTable() + " delete record fail,data: " + event.getData();
            System.out.println(stringBuilder);
        }else
            System.out.println(event.getDatabase() + "-" +
                    event.getTable() + " 删除数据成功");
    }

    @Override
    public boolean isHandle(String database, String table) {
        return database.equals("hot2_bc2mes") && table.startsWith("bc_station_fx_detail_");
    }
}
